{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "## 混合使用 Path、Query 和请求体参数"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:     127.0.0.1:58616 - \"POST /items/123?name=yzpang HTTP/1.1\" 200 OK\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:     Shutting down\n",
      "INFO:     Waiting for application shutdown.\n",
      "INFO:     Application shutdown complete.\n",
      "INFO:     Finished server process [2580]\n",
      "INFO:     Started server process [2580]\n",
      "INFO:     Waiting for application startup.\n",
      "INFO:     Application startup complete.\n",
      "INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:     127.0.0.1:58716 - \"POST /items/123 HTTP/1.1\" 422 Unprocessable Entity\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:     Shutting down\n",
      "INFO:     Waiting for application shutdown.\n",
      "INFO:     Application shutdown complete.\n",
      "INFO:     Finished server process [2580]\n",
      "INFO:     Started server process [2580]\n",
      "INFO:     Waiting for application startup.\n",
      "INFO:     Application startup complete.\n",
      "INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)\n",
      "INFO:     Shutting down\n",
      "INFO:     Waiting for application shutdown.\n",
      "INFO:     Application shutdown complete.\n",
      "INFO:     Finished server process [2580]\n"
     ]
    }
   ],
   "source": [
    "from fastapi import FastAPI, Path, Query\n",
    "from pydantic import BaseModel\n",
    "from typing import Annotated\n",
    "import uvicorn\n",
    "\n",
    "\n",
    "class Item(BaseModel):\n",
    "    name: str\n",
    "    description: str | None = None\n",
    "    price: float\n",
    "    tax: float | None = None\n",
    "\n",
    "app = FastAPI()\n",
    "\n",
    "@app.post(\"/items/{item_id}\")\n",
    "async def update_item(item_id: Annotated[int, Path(title=\"The ID of the item to get\", ge=0, le=1000)],\n",
    "                      name: str | None = None,\n",
    "                      item: Item | None = None):\n",
    "    result = {'item_id': item_id}\n",
    "    if name:\n",
    "       result.update({\"name\": name})\n",
    "    if item:\n",
    "        result.update({\"item\": item})\n",
    "    return result\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    config = uvicorn.Config(app=app, host='127.0.0.1', port=8000)\n",
    "    server = uvicorn.Server(config)\n",
    "    await server.serve()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 多个请求体"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:     Shutting down\n",
      "INFO:     Waiting for application shutdown.\n",
      "INFO:     Application shutdown complete.\n",
      "INFO:     Finished server process [2580]\n",
      "INFO:     Started server process [2580]\n",
      "INFO:     Waiting for application startup.\n",
      "INFO:     Application startup complete.\n",
      "INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:     127.0.0.1:58587 - \"POST /items/123?name=yzpang HTTP/1.1\" 200 OK\n",
      "INFO:     127.0.0.1:58588 - \"POST /items/123?name=yzpang HTTP/1.1\" 200 OK\n",
      "INFO:     127.0.0.1:58589 - \"POST /items/123?name=yzpang HTTP/1.1\" 200 OK\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:     Shutting down\n",
      "INFO:     Waiting for application shutdown.\n",
      "INFO:     Application shutdown complete.\n",
      "INFO:     Finished server process [2580]\n"
     ]
    }
   ],
   "source": [
    "from fastapi import FastAPI, Path, Query\n",
    "from pydantic import BaseModel\n",
    "from typing import Annotated\n",
    "import uvicorn\n",
    "\n",
    "\n",
    "class Item(BaseModel):\n",
    "    name: str\n",
    "    description: str | None = None\n",
    "    price: float\n",
    "    tax: float | None = None\n",
    "\n",
    "class User(BaseModel):\n",
    "    name: str\n",
    "    age: int\n",
    "    address: str | None = None\n",
    "\n",
    "\n",
    "app = FastAPI()\n",
    "\n",
    "@app.post(\"/items/{item_id}\")\n",
    "async def update_item(item_id: Annotated[int, Path(title=\"The ID of the item to get\", ge=0, le=1000)],\n",
    "                      user: User | None = None,\n",
    "                      item: Item | None = None):\n",
    "    result = {'item_id': item_id}\n",
    "    if user:\n",
    "       result.update({\"user\": user})\n",
    "    if item:\n",
    "        result.update({\"item\": item})\n",
    "    return result\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    config = uvicorn.Config(app=app, host='127.0.0.1', port=8000)\n",
    "    server = uvicorn.Server(config)\n",
    "    await server.serve()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 请求体中单一值\n",
    "与使用 Query 和 Path 为查询参数和路径参数定义额外数据的方式相同，FastAPI 提供了一个同等的 Body。\n",
    "\n",
    "例如，为了扩展先前的模型，你可能决定除了 item 和 user 之外，还想在同一请求体中具有另一个键 importance。\n",
    "\n",
    "如果你就按原样声明它，因为它是一个单一值，FastAPI 将假定它是一个查询参数。\n",
    "\n",
    "但是你可以使用 Body 指示 FastAPI 将其作为请求体的另一个键进行处理。"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:     Started server process [2580]\n",
      "INFO:     Waiting for application startup.\n",
      "INFO:     Application startup complete.\n",
      "INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:     127.0.0.1:58776 - \"POST /items/123 HTTP/1.1\" 200 OK\n",
      "INFO:     127.0.0.1:58778 - \"POST /items/123 HTTP/1.1\" 200 OK\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:     Shutting down\n",
      "INFO:     Waiting for application shutdown.\n",
      "INFO:     Application shutdown complete.\n",
      "INFO:     Finished server process [2580]\n"
     ]
    }
   ],
   "source": [
    "from fastapi import FastAPI, Path, Body\n",
    "from pydantic import BaseModel\n",
    "from typing import Annotated\n",
    "import uvicorn\n",
    "\n",
    "class Item(BaseModel):\n",
    "    name: str\n",
    "    description: str | None = None\n",
    "    price: float\n",
    "    tax: float | None = None\n",
    "\n",
    "class User(BaseModel):\n",
    "    name: str\n",
    "    age: int\n",
    "    address: str | None = None\n",
    "\n",
    "app = FastAPI()\n",
    "\n",
    "@app.post(\"/items/{item_id}\")\n",
    "async def update_item(item_id: Annotated[int, Path(title=\"The ID of the item to get\", ge=0, le=1000)],\n",
    "                      user: User | None = None,\n",
    "                      importance: Annotated[int, Body()] = None):\n",
    "    result = {'item_id': item_id}\n",
    "    if user:\n",
    "       result.update({\"user\": user})\n",
    "    if importance:\n",
    "        result.update({\"importance\": importance})\n",
    "    return result\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    config = uvicorn.Config(app=app, host='127.0.0.1', port=8000)\n",
    "    server = uvicorn.Server(config)\n",
    "    await server.serve()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:     Started server process [19048]\n",
      "INFO:     Waiting for application startup.\n",
      "INFO:     Application startup complete.\n",
      "INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:     127.0.0.1:50357 - \"GET /docs HTTP/1.1\" 200 OK\n",
      "INFO:     127.0.0.1:50357 - \"GET /openapi.json HTTP/1.1\" 200 OK\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:     Shutting down\n",
      "INFO:     Waiting for application shutdown.\n",
      "INFO:     Application shutdown complete.\n",
      "INFO:     Finished server process [19048]\n"
     ]
    }
   ],
   "source": [
    "from fastapi import FastAPI, Body\n",
    "from typing import List, Dict\n",
    "import uvicorn\n",
    "\n",
    "app = FastAPI()\n",
    "\n",
    "@app.post(\"/items\")\n",
    "async def read_item(\n",
    "        types: List[str] = Body([\"online\", \"local\"], embed=True, description=\"模型配置匹配类型\"),\n",
    "        kwargs: Dict = Body(embed=False)\n",
    "):\n",
    "    results = {\"types\": types}\n",
    "    return results\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    config = uvicorn.Config(app=app, host='127.0.0.1', port=8000)\n",
    "    server = uvicorn.Server(config)\n",
    "    await server.serve()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}